import util from '@/utils/util'
import {Permission} from '@/utils/resource'
import {ElMessage, ElMessageBox} from "element-plus";

export const confirm = (options, confirmFunc, success, fail, cancel) => {
    let defaulOp = {
        _title: '您确定?',
        _content: '删除的记录将不能恢复!',
        confirmButtonText: '是的, 我要删除!',
        cancelButtonText: '取消',
        type: 'warning',
        successMsg: '删除成功',
        errMsg: '删除失败!'
    }
    let op = Object.assign(defaulOp, options)

    return ElMessageBox.confirm(op._content, op._title, op).then(() => {
        return confirmFunc()
    }).then(response => {
        if (response.code === 0) {
            if (!util.isNull(op.successMsg)) {
                ElMessage.success(op.successMsg);
            }
            success(response)
        } else {
            if (!util.isNull(op.errMsg)) {
                ElMessage.error(op.errMsg);
            }
            fail(response)
        }
    }).catch(() => {
        if (cancel) {
            return cancel()
        }
    })
}

export const validateForm = (formRefValue, formName, success, fail) => {
    return formRefValue.validate((valid) => {
        if (valid) {
            if (success) {
                success()
            }
        } else {
            if (fail) {
                fail()
            } else {
                console.error('validate failed!!')
            }
        }
    })
}

export const showSaveResponse = (loading, saveFunc, success, fail) => {
    loading = true
    saveFunc().then(res => {
        if (res.code === 0) {
            ElMessage.success('保存成功!');
            if (success) {
                setTimeout(() => success(), 500)
            }
        } else {
            ElMessage.error('保存失败');
            if (fail) {
                setTimeout(() => fail(), 500)
            }
        }
        loading = false
    })
}

export const validFieldExists = (rule, value, callback, isValid, validFunc, failFunc) => {
    let fieldName = rule.field
    if (!isValid) {
        callback()
    } else {
        new Promise((resolve) => {
            resolve(validFunc(rule, value))
        }).then(res => {
            if (res.data) {
                callback()
            } else {
                if (failFunc) {
                    failFunc(callback, res.data)
                } else {
                    let errorMsg = util.isNull(res.data.msg) ? `字段: ${fieldName} 已存在` : res.data.msg
                    callback(new Error(errorMsg))
                }
            }
        })
    }
}

const home = {
    componentName: "Dashboard",
    code: "dashboard",
    name: "首页",
    level: 1,
    parent: 0,
    children: [],
    icon: "HomeFilled"
};

export const getMenuTree = async () => {
    let data = await Permission.listMenuTree()
    if (data.code === 0) {
        let menus = data.data
        let menusL1 = []

        menusL1.push(home)
        return menusL1.concat(menus)
    }
}
export const getMenus = async () => {
    let res = await Permission.listMenus()
    if (res.code === 0) {
        let menus = res.data
        let menusL1 = []
        menusL1.push(home)
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];
            if (menu.level === 1) {
                menusL1.push(menu)
            }
        }
        return menusL1
    }
}

